//
//  BDPostRedEnvelopeTableViewCell.m
//  BadBoy
//
//  Created by wen on 2017/7/13.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDPostRedEnvelopeTableViewCell.h"
#import "BDRedEnvelopeModel.h"
#import "BDUserModel.h"

@interface BDPostRedEnvelopeTableViewCell ()

@property (nonatomic, strong) UIView *bkView;
@property (nonatomic, strong) UIView *dashLine;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;

@property (nonatomic, strong) UILabel *tehuiLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *phoneLabel;

@end

@implementation BDPostRedEnvelopeTableViewCell

- (void)configeWithModel:(id)model{
    BDRedEnvelopeModel *redEnvelope = model;
    
    self.imageView.image = [UIImage imageNamed:@"post_hongbao"];
    self.textLabel.text = ASLocalizedString(@"免费查看主题");
    
    self.tehuiLabel.text = ASLocalizedString(@"主题红包");
    self.timeLabel.text = [NSString stringWithFormat:ASLocalizedString(@"%@到期"), redEnvelope.endTimeString];
    BDUserModel *user = [BDUserModel getCurrentUser];
    self.phoneLabel.text = [NSString stringWithFormat:ASLocalizedString(@"限账号手机号为%@"), user.mobile];
    
    CGFloat wid = 120 / 375.0 * kBDScreenWidth;
    
    self.tehuiLabel.font = [UIFont systemFontOfSize:15];
    self.tehuiLabel.textColor = [UIColor colorWithHexString:@"#111111"];
    [self.tehuiLabel sizeToFit];
    self.tehuiLabel.top = 22;
    self.tehuiLabel.left = 15 + wid + 22.5;
    
    self.timeLabel.font = [UIFont systemFontOfSize:12];
    self.timeLabel.textColor = [UIColor colorWithHexString:@"#111111"];
    [self.timeLabel sizeToFit];
    self.timeLabel.top = self.tehuiLabel.bottom + 14;
    self.timeLabel.left = self.tehuiLabel.left;
    
    self.phoneLabel.font = [UIFont systemFontOfSize:12];
    self.phoneLabel.textColor = [UIColor colorWithHexString:@"#111111"];
    [self.phoneLabel sizeToFit];
    self.phoneLabel.top = self.timeLabel.bottom + 4;
    self.phoneLabel.left = self.tehuiLabel.left;
    
    self.contentView.backgroundColor = [UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.bkView.size = CGSizeMake(self.width - 30, self.height);
    self.bkView.centerX = self.width / 2.0;
    
    self.shapeLayer.frame = self.bkView.bounds;
    
    CGFloat radius = 5;
    CGFloat wid = 120 / 375.0 * kBDScreenWidth;
    
    UIBezierPath *bezierpath = [[UIBezierPath alloc] init];
    [bezierpath moveToPoint:CGPointMake(0, radius)];
    [bezierpath addLineToPoint:CGPointMake(0, self.height - radius)];
    [bezierpath addArcWithCenter:CGPointMake(radius, self.height - radius) radius:radius startAngle:M_PI endAngle:M_PI / 2.0 clockwise:NO];
    [bezierpath addLineToPoint:CGPointMake(wid - radius, self.height)];
    [bezierpath addArcWithCenter:CGPointMake(wid, self.height) radius:radius startAngle:M_PI endAngle:0 clockwise:YES];
    [bezierpath addLineToPoint:CGPointMake(self.bkView.width - radius, self.height)];
    [bezierpath addArcWithCenter:CGPointMake(self.bkView.width - radius, self.height - radius) radius:radius startAngle:M_PI / 2.0 endAngle:0 clockwise:NO];
    [bezierpath addLineToPoint:CGPointMake(self.bkView.width, radius)];
    [bezierpath addArcWithCenter:CGPointMake(self.bkView.width - radius, radius) radius:radius startAngle:0 endAngle:-M_PI / 2.0 clockwise:NO];
    [bezierpath addLineToPoint:CGPointMake(wid + radius, 0)];
    [bezierpath addArcWithCenter:CGPointMake(wid, 0) radius:radius startAngle:0 endAngle:M_PI clockwise:YES];
    [bezierpath addLineToPoint:CGPointMake(radius, 0)];
    [bezierpath addArcWithCenter:CGPointMake(radius, radius) radius:radius startAngle:-M_PI / 2.0 endAngle:-M_PI clockwise:NO];
    
    self.shapeLayer.path = bezierpath.CGPath;
    self.shapeLayer.fillColor = [UIColor whiteColor].CGColor;
    self.shapeLayer.shadowColor = [UIColor colorWithHexString:@"#cccccc"].CGColor;
    self.shapeLayer.shadowOffset = CGSizeMake(0, 0);
    self.shapeLayer.shadowOpacity = 1;
    self.shapeLayer.shadowRadius = 1;
    
    self.dashLine.size = CGSizeMake(1, self.height - radius * 2);
    [self.dashLine drawLineLength:5 lineSpacing:2 lineColor:[UIColor colorWithHexString:@"#cccccc"] verticality:YES];
    self.dashLine.top = radius;
    self.dashLine.centerX = self.bkView.left + wid;
    
    self.imageView.size = CGSizeMake(65 / 375.0 * kBDScreenWidth, 50.5 / 375.0 * kBDScreenWidth);
    self.imageView.centerX = self.bkView.left + wid / 2.0;
    self.imageView.centerY = self.height / 2.0 - 10;
    
    self.textLabel.font = [UIFont systemFontOfSize:11];
    self.textLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    [self.textLabel sizeToFit];
    self.textLabel.centerX = self.imageView.centerX;
    self.textLabel.top = self.imageView.bottom + 10;
    
}

- (UIView *)bkView{
    if (!_bkView) {
        self.bkView = [[UIView alloc] init];
        [self.contentView insertSubview:_bkView atIndex:0];
    }
    return _bkView;
}

- (UIView *)dashLine{
    if (!_dashLine) {
        self.dashLine = [[UIView alloc] init];
        [self.contentView addSubview:_dashLine];
    }
    return _dashLine;
}

- (CAShapeLayer *)shapeLayer{
    if (!_shapeLayer) {
        self.shapeLayer = [[CAShapeLayer alloc] init];
        [self.bkView.layer addSublayer:_shapeLayer];
    }
    return _shapeLayer;
}

- (UILabel *)tehuiLabel{
    if (!_tehuiLabel) {
        self.tehuiLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_tehuiLabel];
    }
    return _tehuiLabel;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        self.timeLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_timeLabel];
    }
    return _timeLabel;
}

- (UILabel *)phoneLabel{
    if (!_phoneLabel) {
        self.phoneLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_phoneLabel];
    }
    return _phoneLabel;
}

@end
